import { _decorator, Color, Component, EditBox, Label, Node, Size, UITransform, Vec3 } from 'cc';
import { GameState } from './GameState';
import { EventDispatcher } from './EventDispatcher';
import { Layer1 } from './Layer1';
import { Editor } from './Editor';
const { ccclass, property } = _decorator;

@ccclass('EditorPanel')
export class EditorPanel extends Component {
    @property(Label)
    LabelMode:Label = null;
    @property(Label)
    LabelNum:Label = null;
    @property(Label)
    LabelPosition:Label = null;
    @property(Label)
    LabelHint:Label = null;
    @property(EditBox)
    EditBoxX:EditBox = null;
    @property(EditBox)
    EditBoxY:EditBox = null;
    @property(EditBox)
    EditBoxData:EditBox = null;
    @property(Layer1)
    layer1:Layer1 = null;
    start() {
        this.refreshMode();
        EventDispatcher.getInstTarget().on(EventDispatcher.UPDATE_BLOCK_SIZE, this.updateBlockSize, this);
    }
    onDestroy() {
        EventDispatcher.getInstTarget().off(EventDispatcher.UPDATE_BLOCK_SIZE, this.updateBlockSize, this);
    }
    updateBlockSize() {
        const size = this.layer1.getChildrenLength();
        this.LabelNum.string = size.toString();
        if(size % 3 === 0){
            this.LabelNum.color = new Color(255, 255, 255);
        }else{
            this.LabelNum.color = new Color(255, 0, 0);
        }
    }
    refreshMode() {
        switch(GameState.editMode){
            case 1:
                this.LabelMode.string = '添加方块模式'
                break;
            case 2:
                this.LabelMode.string = '删除方块模式'
                break;
            case 3:
                this.LabelMode.string = '添加网点模式'
                break;
            case 4:
                this.LabelMode.string = '删除网点模式'
                break;
            
        }
        
    }
    onModeChange(e: Event, mode: string) {
        GameState.editMode = Number(mode);
        this.refreshMode();
    }

    onClear(){
        this.layer1.clearAll();
        this.updateBlockSize();
        EventDispatcher.getInstTarget().emit(EventDispatcher.SHOW_TIPS, '清理成功！')
    }
    setPos(x: number, y: number) {
        this.LabelPosition.string = `${x},${y}`;
    }
    onAddCustomGrid() {
        const x = this.EditBoxX.string;
        const y = this.EditBoxY.string;
        const editor = this.node.parent.getChildByName('Editor');
        editor.getComponent(Editor).createGrid(Number(x), Number(y), new Size(10, 10));
    }
    onExport() {
        let str: any[] = [];
        const nodes = this.layer1.node.children;
        if(nodes.length % 3 !== 0 || nodes.length === 0){
            this.LabelHint.string = '必须是3的倍数';
            return;
        }
        nodes.map(node => {
            str.push({
                x: node.position.x,
                y: node.position.y
            })
        })
        this.EditBoxData.string = JSON.stringify(str);
        this.LabelHint.string = '导出成功！';
    }
    onImport() {
        this.layer1.clearAll();
        const data = this.EditBoxData.string;
        if(!data){
            this.LabelHint.string = '数据为空';
            return;
        }
        const grids = JSON.parse(data);
        grids.map(grid => {
            const worldPosition = this.layer1.node.getComponent(UITransform).convertToWorldSpaceAR(new Vec3(grid.x, grid.y, 0))
            this.layer1.addBlock(worldPosition);
        })
        this.layer1.refreshShadow();
        this.updateBlockSize();
    }
}


